﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Web;
using System.Threading;
using WindowsFormsApplication1.Properties;

namespace WindowsFormsApplication1
{
    class HTTPC
    {
        public delegate void Procedure1();
        public delegate void Procedure(string text);    
        byte[] bytes;
        Procedure func = BKUEDITOR.debug1;  
        public static string ziel = Resources.url;

        public void seteCallback( Procedure f)
        {
            func += f;
        }   
        void enable(bool v) {
            //BKUEDITOR.ActiveForm.Enabled = v;       
        }

        public void xmlSig(string text)
        {
            HttpPost(Resources.xmla +  text +  Resources.xmlb);
        }

        public void cmsSig(string text)
        {
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            string c = System.Convert.ToBase64String(enc.GetBytes(text));
            HttpPost(Resources.cmsa+c+Resources.cmsb);
        }

        public void raw(string text) { HttpPost(text); }
        public void nullCmd()        { HttpPost(Resources.nullcmd); }
        public void pruefenXML(String a)
        {
            HttpPost(Resources.checkxmla+a+Resources.checkxmlb);
        }
        
        private void Send(){          
            try
            {
                ServicePointManager.ServerCertificateValidationCallback = 
                    new System.Net.Security.RemoteCertificateValidationCallback(AcceptAllCertifications);

                WebRequest webRequest = WebRequest.Create(ziel);
                webRequest.ContentType = "application/x-www-form-urlencoded";
                webRequest.Method = "POST";  
                webRequest.ContentLength = bytes.Length;             
                Stream os = webRequest.GetRequestStream();
                os.Write(bytes, 0, bytes.Length);
                WebResponse webResponse = webRequest.GetResponse();
                StreamReader sr = new StreamReader(webResponse.GetResponseStream());             
                func(sr.ReadToEnd().Trim());
                
                enable(true);
            }
            catch (WebException ex)
            {
                   MessageBox.Show(ex.Message + ex.StackTrace, "Verbindungsfehler",
                   MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            enable(true);       
        }
        
        public void HttpPost(string parameters)
        {           
            enable(false); 
            BKUEDITOR.debug(parameters);
            parameters = HttpUtility.UrlEncode(parameters);
            parameters="XMLRequest=" + parameters + "\n\n";           
            parameters=parameters.Replace("\n", "\r\n");
            bytes = Encoding.UTF8.GetBytes(parameters);            
        
            ThreadStart job = new ThreadStart(Send);
            Thread thread = new Thread(job);
            thread.Start();           
        }
        public bool AcceptAllCertifications(object sender, System.Security.Cryptography.X509Certificates.X509Certificate certification, System.Security.Cryptography.X509Certificates.X509Chain chain, System.Net.Security.SslPolicyErrors sslPolicyErrors)
        {
            return true;
        }

      
    }
}
